package com.sdp.th.db.th_eq_state.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.core.bizc.db.IViewQuery;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.right.utils.RightConstants;
import com.sdp.th.db.th_eq_state.mapper.ThEqStateMapper;
import com.sdp.th.db.th_eq_state.service.IThEqStateService;
import com.sdp.th.entity.ThEqState;

/**
 * <p>
 * 设备实时信息 服务实现类
 * </p>
 *
 * @author zsp
 * @since 2025-04-12
 */
@Service
public class ThEqStateServiceImpl extends ServiceImpl<ThEqStateMapper, ThEqState> implements IThEqStateService,IViewQuery {

	@Autowired
	private ThEqStateMapper thEqStateMapper;
	
	@Override
	public ThEqState updateState(ThEqState state) {
		ThEqState dbState = queryThEqStateByNo(state.getDeviceNo());
		if(dbState!=null) {
			BigDecimal dbCount = dbState.getAlarmCount()==null?BigDecimal.ZERO:dbState.getAlarmCount();
			
			dbState.setReportTime(state.getReportTime());
			
			if(state.getDeviceHumidity()!=null) {
				dbState.setDeviceHumidity(state.getDeviceHumidity());
			}
			
			if(state.getDeviceTemperature()!=null) {
				dbState.setDeviceTemperature(state.getDeviceTemperature());
			}
			
			if(state.getDevicePower()!=null) {
				dbState.setDevicePower(state.getDevicePower());
			}
			
			if(state.getDeviceSignal()!=null) {
				dbState.setDeviceSignal(state.getDeviceSignal());
			}
		
			dbState.setLineState(state.getLineState());
			dbState.setAlarmState(state.getAlarmState());
			
			if(state.getAlarmState().equals(RightConstants.是.getCode())) {
				dbState.setAlarmCount(dbCount.add(BigDecimal.ONE));
			}
			updateById(dbState);
		}
		return dbState;
	
		
	}

	@Override
	public ThEqState queryThEqStateByNo(String deviceNo) {
		LambdaQueryWrapper<ThEqState> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.eq(ThEqState::getDeviceNo, deviceNo);
		return getOne(lambdaQueryWrapper,false);
	}

	@Override
	public void updateLineState(String deviceNo,String lineState) {
		LambdaUpdateWrapper<ThEqState> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
		lambdaUpdateWrapper.set(ThEqState::getLineState, lineState);
		lambdaUpdateWrapper.eq(ThEqState::getDeviceNo, deviceNo);
		update(lambdaUpdateWrapper);
	}

	@Override
	public <T> List<T> viewList(QueryWrapper<T> queryWrapper, DataCenter dc) {
		return thEqStateMapper.viewList(queryWrapper, dc);
	}

	@Override
	public <T> int viewCount(QueryWrapper<T> queryWrapper) {
		return thEqStateMapper.viewCount(queryWrapper);
	}

	@Override
	public <T> IPage<T> viewPage(IPage<T> page, QueryWrapper<T> queryWrapper, DataCenter dc) {
		return thEqStateMapper.viewPage(page, queryWrapper, dc);
	}

	@Override
	public void delEqStateByNo(String deviceNo) {
		LambdaUpdateWrapper<ThEqState> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
		lambdaUpdateWrapper.eq(ThEqState::getDeviceNo, deviceNo);
		remove(lambdaUpdateWrapper);
		
	}

}
